import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { defalutImg } from '../../../comm';
import { PermissionService } from '../../../service/permission.service';
import { GoodsWatermarkManageService } from './goods-watermark-manage.service';

@Component({
  selector: 'app-goods-watermark-list',
  templateUrl: './goods-watermark-list.component.html',
  styleUrls: ['./goods-watermark-list.component.scss']
})
export class GoodsWatermarkListComponent implements OnInit {

  // 路由缓存恢复时
  static updateCache: any = null;

  fallback: string = defalutImg;
  searchOptions = [
    { type: 1, labelText: '名称', valueKey: 'name', value: '' },
    {
      type: 2,
      labelText: '状态',
      valueKey: 'status',
      value: '',
      multiple: false,
      notTostr: false,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [
        {value: '1', label: '启用'},
        {value: '0', label: '禁用'}
      ]
    }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  constructor(
    private message: NzMessageService,
    private router: Router,
    public permission: PermissionService,
    private services: GoodsWatermarkManageService
  ) { }

  ngOnInit(): void {
    GoodsWatermarkListComponent.updateCache = () => {
      this.loadTable();
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.queryGoodsWaterList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      this.tableParams.tableData = data?.records || [];
      this.tableParams.total = data?.total || 0;
    } else {
      this.message.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  jumpPage(type: number, item?: any) {
    let params: any = {
      type
    }

    if ([1, 2].includes(type)) {
      params.id = item.id;
    }
    this.router.navigate(['/goods/goods-watermark-edit'], {
      queryParams: {
        ...params
      }
    })
  }
}
